package com.company.bookstore.validator.user_admin_module;

import com.company.bookstore.exception.user_admin_module.CustomException;
import com.company.bookstore.qo.user_admin_module.UserQo;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * UserQoUpdateRegisterValidator
 *
 * @blame Android Team
 */
@Component
public class UserQoUpdateRegisterValidator extends AbstractValidatorHandler implements InitializingBean {
    static final Pattern USER_PHONE_PATTERN = Pattern.compile("\\d{11,13}");
    static final Pattern USER_MAIL_PATTERN = Pattern.compile("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");
    static final Pattern USER_ADDRESS_PATTERN = Pattern.compile("^[\\u4e00-\\u9fa5_a-zA-Z0-9-]+$");
    static final Pattern USER_STATUS_PATTERN = Pattern.compile("[0,1]+");
    @Override
    public void afterPropertiesSet() throws Exception {
        ValidatorFactory.register(this.getClass().getName(),this);
    }

    @Override
    public boolean doValidator(Object object) throws NoSuchMethodException, CustomException {

        UserQo userQo = (UserQo)object;
        if (userQo.getId()==null){
            throw  new CustomException("用户id不能为空",405);
        }
        Matcher correctUsername;
        Matcher correctPassword;
        if (!StringUtils.isEmpty(userQo.getUsername())){
            correctUsername = UserQoRegisterValidator.USERNAME_PATTERN.matcher(userQo.getUsername());
            if (!correctUsername.matches()) {
                throw new CustomException("账号账户名格式不正确，要求8-20位字母，数字，不能含有标点符号", 405);
            }
        }
        if (!StringUtils.isEmpty(userQo.getPassword())){
            correctPassword = UserQoRegisterValidator.PASSWORD_PATTERN.matcher(userQo.getPassword());
            if (!correctPassword.matches()){
                throw  new CustomException("账号密码格式不正确，要求8-20位字母，数字，可含有标点符号",405);
            }
        }
        if (!StringUtils.isEmpty(userQo.getPhone())){
            Matcher correctPhone = USER_PHONE_PATTERN.matcher(userQo.getPhone());
            if (!correctPhone.matches()){
                throw  new CustomException("账号手机号不正确，要求数字11到12位",405);
            }
        }
        if (!StringUtils.isEmpty(userQo.getEmail())){
            Matcher correctMail = USER_MAIL_PATTERN.matcher(userQo.getEmail());
            if (!correctMail.matches()){
                throw  new CustomException("账号邮件地址不正确，要求xxx@xx.xx, x只允许字母，数字，-，_",405);
            }
        }
        if (!StringUtils.isEmpty(userQo.getTempAdd())){
            Matcher correctAddress  = USER_ADDRESS_PATTERN.matcher(userQo.getTempAdd());
            if (!correctAddress.matches()){
                throw  new CustomException("账号默认收件地址不正确，要求字母，数字，中文,-,_",405);
            }
        }
        if (userQo.getDeleted()!=null){
            boolean correctDeleted  = USER_STATUS_PATTERN.matcher(userQo.getDeleted().toString()).matches();
            if (!correctDeleted){
                throw new CustomException("账户状态不正确，只能为 0或1",405);
            }
        }
        if (userQo.getEnable()!=null){
            boolean correctEnable = USER_STATUS_PATTERN.matcher(userQo.getEnable().toString()).matches();
            if (!correctEnable){
                throw new CustomException("账户状态不正确，只能为 0或1",405);
            }
        }
        return true;
    }
}
